<template>
  <div class="app-container">
    <div>
      <el-button
        :type="show == '1' ? 'primary' : ''"
        size="medium"
        @click="changeShow(1)"
        >Bug新建表</el-button
      >
      <el-button
        :type="show == '2' ? 'primary' : ''"
        size="medium"
        @click="changeShow(2)"
        >Bug验证表</el-button
      >
      <el-button
        :type="show == '3' ? 'primary' : ''"
        size="medium"
        @click="changeShow(3)"
        >Bug关闭表</el-button
      >
    </div>
    <New v-if="show == '1'"></New>
    <Checking v-if="show == '2'"></Checking>
    <Close v-if="show == '3'"></Close>
  </div>
</template>

<script>
import * as bug from "@/api/bug";
import New from "./new";
import Checking from "./checking.vue";
import Close from "./close";
import { time } from "echarts";
export default {
  data() {
    return {
      show: "1",
      options: [],
      value: [],
      radio: 0,
      radio2: 0,
      milestone_verison: "",
      subsystem: "",
      verison_NO: "",
      tester_name: "",
      date_range: "",
      select: null,
      Bug_detail_list: [],
      checked: [],
      tiem: [],
      tiem2: [],
      pageNo: 0,
      total: 0,
      pageNum: 25,
    };
  },
  components: {
    New,
    Checking,
    Close,
  },
  methods: {
    changeShow(i) {
      this.show = i;
    },
    pChange(val) {
      if (val) {
        bug.SubsysList("?project_id=" + val).then((res) => {
          this.$set(this.options, 4, res.data.subsys_names);
        });
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = val;
      this.getBugStatistics();
    },
    handleCurrentChange(val) {
      this.pageNo = val - 1;
      this.getBugStatistics();
      console.log(`当前页: ${val}`);
    },

    sortMethod(a, b) {
      return a.localeCompare(b);
    },
    sortChange({ column, prop, order }) {
      this.Bug_detail_list.sort((a, b) => {
        if (order === "ascending") {
          return this.sortMethod(a[prop], b[prop]);
        } else if (order === "descending") {
          return this.sortMethod(b[prop], a[prop]);
        } else {
          return this.sortMethod(a[prop], b[prop]);
        }
      });
    },
    handleCheckedCitiesChange(value) {
      console.log(this.checked);
      if (value) {
        for (let i in this.Bug_detail_list) {
          this.$set(this.checked, i, true);
        }
      } else {
        for (let i in this.Bug_detail_list) {
          this.$set(this.checked, i, false);
        }
      }
      console.log(this.checked);
    },
    download() {
      let data = [];
      for (let i in this.checked) {
        if (this.checked[i]) {
          data.push(this.Bug_detail_list[i].Bug_id);
        }
      }

      bug.download({ Bug_ids: data }).then((res) => {
        let blob = res;
        let reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onload = (e) => {
          let a = document.createElement("a");
          a.download = "bug_file.csv";
          a.href = e.target.result;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        };
      });
    },
    async getBugStatistics() {
      // let v5 = "";
      // switch (this.value[5]) {
      //   case "已修复":
      //     v5 = "fixed";
      //     break;
      //   case "不存在":
      //     v5 = "notrepro";
      //     break;
      //   case "外部原因":
      //     v5 = "external";
      //     break;
      //   case "不予修复":
      //     v5 = "willnotfix";
      //     break;
      //   case "设计如此":
      //     v5 = "bydesign";
      //     break;
      //   case "重复提交":
      //     v5 = "duplicate";
      //     break;
      //   case "待解决":
      //     v5 = "tostory";
      // }
      let v0 = -1;
      if (this.value[0] || this.value[0] == 0) v0 = this.value[0];

      let data = {
        project_id: v0,
        creator_name: this.value[1] || "",
        assigner_name: this.value[2] || "",
        Bug_status: this.value[3] || "",
        subsys_id: this.value[4] || "",
        solve_resolution: this.value[5],
        date_create_start: this.tiem[0] || "",
        date_create_end: this.tiem[1] || "",
        date_solve_start: this.tiem2[0] || "",
        date_solve_end: this.tiem2[1] || "",
        pageNo: this.pageNo,
        pageNum: this.pageNum,
      };
      let res = await bug.BugStatistics(data);
      if (res.data.Bug_detail_list) {
        this.total = res.data.Bug_total;
        this.Bug_detail_list = res.data.Bug_detail_list;
      }
    },
    next(f) {
      if (f) this.pageNo = 0;
      else this.pageNo++;
      this.getBugStatistics();
    },
    upper() {
      this.pageNo--;
      if (this.pageNo < 0) this.pageNo = 0;
      this.getBugStatistics();
    },
  },
  created() {
    // this.getBugStatistics();
    // setTimeout(() => {
    //   bug.projectList().then((res) => {
    //     this.$set(this.options, 0, res.data.project_names);
    //   });
    // }, 1600);
    // setTimeout(() => {
    //   bug.BugResolutionList().then((res) => {
    //     this.$set(this.options, 5, res.data.Bug_status);
    //   });
    // });
    // setTimeout(() => {
    //   bug.creatorList().then((res) => {
    //     this.$set(this.options, 1, res.data.creator_names);
    //   });
    //   bug.SubsysList().then((res) => {
    //     this.$set(this.options, 4, res.data.subsys_names);
    //   });
    // }, 600);
    // setTimeout(() => {
    //   bug.assignerList().then((res) => {
    //     this.$set(this.options, 2, res.data.assigner_names);
    //   });
    //   bug.BugstatusList().then((res) => {
    //     this.$set(this.options, 3, res.data.Bug_status);
    //   });
    // }, 1000);
  },
};
</script>

<style scoped>
#sjlx span {
  margin-right: 30px;
}

#sx {
  margin-top: 20px;
}
#sx .el-row {
  margin: 20px 0;
}

#data {
  margin: 20px 20px;
}
</style>
